using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebControls;
using BusinessLayer;
using InfraStructureLayer;
using EntityLayer;

public partial class blog_Default : SFPage
{
    private DataTable Entries = null;
    protected bool sfrDays_NeedsDataSource()
    {
        busEntries insBusEntries = new busEntries();
        DataTable insDataTableEntries = null;

        if (this.QueryStringArchive.ToString() == "")
        {
            if (Request.QueryString["Archive"] != null && Request.QueryString["Archive"] != "")
            {
                this.QueryStringArchive = Request.QueryString["Archive"];
            }
        }
        if (this.QueryStringCategory.ToString() == "")
        {
            if (Request.QueryString["Category"] != null && Request.QueryString["Category"] != "")
            {
                this.QueryStringCategory = Request.QueryString["Category"];
            }
        }

        if (this.QueryStringArchive == "" && this.QueryStringCategory == "")
        {
            insDataTableEntries = insBusEntries.SelectEntryDates();
            Entries = insBusEntries.SelectEntriesBelongsToDates();
        }
        else if (this.QueryStringCategory != "")
        {
            //entArticles insEntArticles = new entArticles();
            string categoryId = new Crypto().DecryptURL(this.QueryStringCategory);
            insDataTableEntries = insBusEntries.SelectEntryDatesByCategoryId(Convert.ToInt32(categoryId));
            Entries = insBusEntries.SelectEntriesBelongsToDatesByCategoryId(Convert.ToInt32(categoryId));
        }
        else
        {
            entEntries insEntEntries = new entEntries();
            string archive = new Crypto().DecryptURL(this.QueryStringArchive);
            string year = archive.Substring(0, 4);
            string month = "";
            if (archive.Substring(4, 2).StartsWith("0"))
            {
                month = archive.Substring(5, 1);
            }
            else
            {
                month = archive.Substring(4, 2);
            }
            insEntEntries.PublishDate = new DateTime(Convert.ToInt32(year), Convert.ToInt32(month), 1);
            insDataTableEntries = insBusEntries.SelectEntryDatesByPublishDate(insEntEntries);
            Entries = insBusEntries.SelectEntriesBelongsToDatesByPublishDate(insEntEntries);
        }
        if (insBusEntries.HasErrors)
        {
            MessageBoxShow(insBusEntries);
            return true;
        }
        Entries.Columns.Add("PublishDateString", typeof(string));
        foreach (DataRow dr in Entries.Rows)
        {
            dr["PublishDateString"] = Convert.ToDateTime(dr["PublishDate"]).ToShortDateString();
        }
        sfrDays.SetData(insDataTableEntries);
        return insBusEntries.HasErrors;
    }
    public void sfrDays_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
    {
        if (e.Item.DataItem != null)
        {
            DateTime d = Convert.ToDateTime(((DataRowView)e.Item.DataItem)["PublishDate"]);
            DataRow[] drArr = Entries.Select("PublishDateString='" + d.ToShortDateString() + "'");
            DataTable dt = Entries.Clone();
            foreach (DataRow dr in drArr)
            {
                dt.Rows.Add(dr.ItemArray);
            }
            ((SFRepeater)e.Item.FindControl("sfrEntries")).DataSource = dt;
            ((SFRepeater)e.Item.FindControl("sfrEntries")).DataBind();
        }
    }
    protected void sfrEntries_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Comments")
        {
            this.Id = Convert.ToInt32(e.CommandArgument);
            this.OpenEditPanel();
        }
    }
    //public override void OpenEditPanel(int height, int width)
    //{
    //    this.Clear(this.Master.FindControl("ContentPlaceHolderEdit"));
    //    base.OpenEditPanel(height, width);
    //    sfrComments.LoadRepeaterData();
    //}

    protected bool sfrComments_NeedsDataSource()
    {
        busComments insBusComments = new busComments();
        entComments insEntComments = new entComments();
        insEntComments.EntryContentId = this.Id;
        DataTable insDataTableComments = insBusComments.SelectCommentsByEntryContentId(insEntComments);
        if (insBusComments.HasErrors)
        {
            MessageBoxShow(insBusComments);
            return true;
        }
        sfrComments.SetData(insDataTableComments);

        pnlAddComment.Visible = false;
        pnlComments.Visible = true;
        this.Master.FindControl("btnSave").Visible=false;
        this.Master.FindControl("btnClose").Visible = false;
        return insBusComments.HasErrors;
    }
    protected void sfrComments_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "DeleteComment")
        {
            entComments insEntComments = new entComments();
            insEntComments.CommentId = Convert.ToInt32(e.CommandArgument);
            busComments insBusComments = new busComments();
            insBusComments.DeleteCommentsByCommentId(insEntComments);
            if (insBusComments.HasErrors)
            {
                MessageBoxShow(insBusComments);
                return;
            }

            sfrComments.LoadRepeaterData();

        }
    }
    protected void lblAddComment_Click(object sender, EventArgs e)
    {
        pnlAddComment.Visible = true;
        pnlComments.Visible = false;
        this.Master.FindControl("btnSave").Visible = true;
        this.Master.FindControl("btnClose").Visible = true;
        this.OpenEditPanel();
    }
    protected void lblShowComment_Click(object sender, EventArgs e)
    {
        this.OpenEditPanel();
    }
    public override void Save()
    {
        Validate();
        if (this.HasErrors)
        {
            MessageBoxShow(this);
            return;
        }
        if (Session["SecurityPicture"].ToString() != txtSecurityPicture.Text)
        {
            MessageBoxShow(StaticMethods.GetErrorMessageMultilanguageString(45, new object[] { }));
            return;
        }
        if (txtCommentUserEmail.Text.Trim().Length > 0)
        {
            if (!WebControls.Validation.ValidateEmail(txtCommentUserEmail.Text))
            {
                MessageBoxShow(StaticMethods.GetErrorMessageMultilanguageString(18, new object[] { lblCommentUserEmail.Text }));
                return;
            }
        }
        if (txtCommentUserWebSite.Text.Trim().Length > 0)
        {
            if (!WebControls.Validation.ValidateWebSite(txtCommentUserWebSite.Text))
            {
                MessageBoxShow(StaticMethods.GetErrorMessageMultilanguageString(18, new object[] { lblCommentUserWebSite.Text }));
                return;
            }
        }

        entComments insEntComments = new entComments();
        insEntComments.CommentBody = txtCommentBody.Text;
        insEntComments.CommentDate = DateTime.Now;
        insEntComments.CommentUserEMail = txtCommentUserEmail.Text;
        insEntComments.CommentUserIPAddress = Request.UserHostAddress;
        insEntComments.CommentUserName = txtCommentUserName.Text;
        insEntComments.CommentUserWebSite = txtCommentUserWebSite.Text;
        insEntComments.EntryContentId = this.Id;
        busComments insBusComments = new busComments();
        insBusComments.InsertComments(insEntComments);
        if (insBusComments.HasErrors)
        {
            MessageBoxShow(insBusComments);
            return;
        }
        base.SaveNew();
        this.OpenEditPanel();
        
    }
    protected string GenerateUrl(object obj)
    {
        return "~/" + CurrentContext.Language + "/blog/" + obj.ToString() + ".aspx";
    }
    protected bool GetVisible()
    {
        return (CurrentContext.UserId != null);
    }
}
